package com.tqjc.execute.task.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.additional.update.impl.LambdaUpdateChainWrapper;
import com.tqjc.execute.task.entity.TaskStep;
import com.tqjc.execute.task.mapper.TaskStepMapper;
import com.tqjc.execute.task.service.TaskStepService;
import com.tqjc.execute.utils.PublicResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: TODO
 * @author: pzy
 * @date: 2023年05月17日 9:30
 */

@Service
public class TaskStepServiceImpl implements TaskStepService {
    @Resource
    private TaskStepMapper taskStepMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public TaskStep queryById(Integer id) {
        return taskStepMapper.selectById(id);
    }

    /**
     * 分页查询
     *
     * @param taskStep 筛选条件
     * @param current  当前页码
     * @param size     每页大小
     * @return
     */
    @Override
    public Page<TaskStep> paginQuery(TaskStep taskStep, long current, long size) {
        //1. 构建动态查询条件
        LambdaQueryWrapper<TaskStep> queryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(taskStep.getTaskType())) {
            queryWrapper.eq(TaskStep::getTaskType, taskStep.getTaskType());
        }
        //2. 执行分页查询
        Page<TaskStep> pagin = new Page<>(current, size, true);
        IPage<TaskStep> selectResult = taskStepMapper.selectByPage(pagin, queryWrapper);
        pagin.setPages(selectResult.getPages());
        pagin.setTotal(selectResult.getTotal());
        pagin.setRecords(selectResult.getRecords());
        //3. 返回结果
        return pagin;
    }

    /**
     * 新增数据
     *
     * @param taskStep 实例对象
     * @return 实例对象
     */
    @Override
    public TaskStep insert(TaskStep taskStep) {
        taskStepMapper.insert(taskStep);
        return taskStep;
    }

    /**
     * 更新数据
     *
     * @param taskStep 实例对象
     * @return 实例对象
     */
    @Override
    public TaskStep update(TaskStep taskStep) {
        //1. 根据条件动态更新
        LambdaUpdateChainWrapper<TaskStep> chainWrapper = new LambdaUpdateChainWrapper<TaskStep>(taskStepMapper);
        if (StrUtil.isNotBlank(taskStep.getTaskType())) {
            chainWrapper.eq(TaskStep::getTaskType, taskStep.getTaskType());
        }
        //2. 设置主键，并更新
        chainWrapper.set(TaskStep::getId, taskStep.getId());
        boolean ret = chainWrapper.update();
        //3. 更新成功了，查询最最对象返回
        if (ret) {
            return queryById(taskStep.getId());
        } else {
            return taskStep;
        }
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        int total = taskStepMapper.deleteById(id);
        return total > 0;
    }


    /**
     * 通过任务类型获取所有的步骤
     *
     * @param taskType 任务类型
     * @return PublicResult
     */
    @Override
    public PublicResult<List<String>> selectStepsByTaskType(String taskType) {
        List<String> steps = taskStepMapper.selectStepsByTaskType(taskType);
        if (steps.size() > 0) {
            return PublicResult.ok(steps);
        }
        return PublicResult.fail("没有找到步骤!");
    }
}
